/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.oa.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.act.service.ActTaskService;
import com.thinkgem.jeesite.modules.act.utils.ActUtils;
import com.thinkgem.jeesite.modules.oa.dao.OaLeaveDao;
import com.thinkgem.jeesite.modules.oa.entity.OaLeave;

/**
 * 请假流程Service
 * @author liulin
 * @version 2018-04-08
 */
@Service
@Transactional(readOnly = true)
public class OaLeaveService extends CrudService<OaLeaveDao, OaLeave> {

	@Autowired
	private ActTaskService actTaskService;
	
	public OaLeave get(String id) {
		return super.get(id);
	}
	
	public List<OaLeave> findList(OaLeave oaLeave) {
		return super.findList(oaLeave);
	}
	
	public Page<OaLeave> findPage(Page<OaLeave> page, OaLeave oaLeave) {
		return super.findPage(page, oaLeave);
	}
	
	@Transactional(readOnly = false)
	public void save(OaLeave oaLeave) {
		super.save(oaLeave);
	}
	/**
	 * 启动流程
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public void save(OaLeave leave, Map<String, Object> variables) {
		
		// 保存业务数据
		if (StringUtils.isBlank(leave.getId())){
			leave.preInsert();
			dao.insert(leave);
			// 启动流程
			actTaskService.startProcess(ActUtils.PD_LEAVE[0], ActUtils.PD_LEAVE[1], leave.getId(), leave.getReason());

		}else{
			leave.preUpdate();
			dao.update(leave);
			
			Map<String, Object> vars = Maps.newHashMap();
			if("audit".equals(leave.getAct().getProcDefKey())) {
				leave.getAct().setComment(("yes".equals(leave.getAct().getFlag())?"[同意] ":"[驳回] ")+leave.getAct().getComment());
			}
			// 完成流程任务
			vars.put("pass", "yes".equals(leave.getAct().getFlag())? "1" : "0");
			actTaskService.complete(leave.getAct().getTaskId(), leave.getAct().getProcInsId(), leave.getAct().getComment(), vars);
		}
	}
	@Transactional(readOnly = false)
	public void delete(OaLeave oaLeave) {
		super.delete(oaLeave);
	}
	
}